package DaoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Dao.ActivityDao;
import Entity.Activity;
import Entity.Stlist;
import Entity.User;
import Util.HibernateUtil;

public class ActivityDaoImpl implements ActivityDao {
   //按id查找活动
	@Override
	public Activity findActivity(String id) {
		Transaction ts = null;
		String hql;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			hql = "from Activity where activityId=?";
			Query query = session.createQuery(hql);
			query.setParameter(0, id);
			List<Activity> listActivity = query.list();
			ts.commit();
			return listActivity.get(0);
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return null;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
		}
	}
   //某个社团新增活动
	@Override
	public boolean addActivity(Activity activity, String stid) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
		    Stlist st=(Stlist) session.get(Stlist.class, stid);
			st.getSt_aclist().add(activity);
			session.save(activity);
			session.save(st);
			ts.commit();
			return true;
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return false;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
		}
	}
   //更新活动内容
	@Override
	public boolean updateActivity(Activity activity) {
		Transaction ts=null;
		try {
			Session session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.update(activity);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
		}
	}
  //某个社团删除活动
	@Override
	public boolean deleteActivity(Activity activity, String stid) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			session.delete(activity);
		    Stlist st=(Stlist) session.get(Stlist.class, stid);
			st.getSt_aclist().remove(activity);
			session.save(st);
			ts.commit();
			return true;
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return false;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
		}
	}
    //查找某个社团的全部活动
	@Override
	public List<Activity> findAllActivityByStid(String stid) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			List<Activity> list=new ArrayList<Activity>();
		    Stlist st=(Stlist) session.get(Stlist.class, stid);
			list=(List<Activity>) st.getSt_aclist();
			ts.commit();
			return list;
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return null;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
		}
	}
  //查找全部活动
	@Override
	public List<Activity> findAllActivity() {
		Transaction ts = null;
		String hql;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			hql = "from Activity";
			Query query = session.createQuery(hql);
			List<Activity> listActivity = query.list();
			ts.commit();
			return listActivity;
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return null;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
		}
	}


	

}
